using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for ImageHandler
/// </summary>
public class ImageHandler : IHttpHandler
{
	public ImageHandler()
	{
		
	}

	#region [ IHttpHandler Members ]

	public bool IsReusable
	{
		get { return true; }
	}

	public void ProcessRequest(HttpContext context)
	{
		if (context.Request.QueryString == null || context.Request.QueryString.Count == 0) return;
		string id = context.Request.QueryString["id"];
		if (string.IsNullOrEmpty(id) || id.Length > 40) return;
		string SecurityToken;
		string errorMessage;
		byte[] img;
		MyRealNet.CaptchaService.CaptchaService captcha = new MyRealNet.CaptchaService.CaptchaService();
		if (!captcha.GetCaptchaImage(out img, out SecurityToken, out errorMessage))
			return;
		if (context.Cache[id] != null) context.Cache.Remove(id);
		context.Cache.Insert(id, SecurityToken, null, DateTime.Now.AddMinutes(30), System.Web.Caching.Cache.NoSlidingExpiration);
		using (System.IO.MemoryStream stream2 = new System.IO.MemoryStream(img))
		{
			//----------------------------------------
			context.Response.ContentType = "image/jpeg";
			stream2.WriteTo(context.Response.OutputStream);
			//----------------------------------------
		}
	}

	#endregion
}
